using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace Microsoft.Phone.Controls.Primitives
{
	[TemplatePart(Name = "Transform", Type = typeof(TranslateTransform)), TemplateVisualState(GroupName = "Common", Name = "Expanded"), TemplateVisualState(GroupName = "Common", Name = "Selected"), TemplateVisualState(GroupName = "Common", Name = "Normal")]
	public class LoopingSelectorItem : ContentControl
	{
		internal enum State
		{
			Normal,
			Expanded,
			Selected
		}
		private const string TransformPartName = "Transform";
		private const string CommonGroupName = "Common";
		private const string NormalStateName = "Normal";
		private const string ExpandedStateName = "Expanded";
		private const string SelectedStateName = "Selected";
		private bool _shouldClick;
		private LoopingSelectorItem.State _state;
		public event EventHandler<EventArgs> Click;
		internal LoopingSelectorItem Previous
		{
			get;
			private set;
		}
		internal LoopingSelectorItem Next
		{
			get;
			private set;
		}
		internal TranslateTransform Transform
		{
			get;
			private set;
		}
		public LoopingSelectorItem()
		{
			base.set_DefaultStyleKey(typeof(LoopingSelectorItem));
			base.add_MouseLeftButtonDown(new MouseButtonEventHandler(this.LoopingSelectorItem_MouseLeftButtonDown));
			base.add_MouseLeftButtonUp(new MouseButtonEventHandler(this.LoopingSelectorItem_MouseLeftButtonUp));
			base.add_LostMouseCapture(new MouseEventHandler(this.LoopingSelectorItem_LostMouseCapture));
			base.add_Tap(new EventHandler<GestureEventArgs>(this.LoopingSelectorItem_Tap));
		}
		internal void SetState(LoopingSelectorItem.State newState, bool useTransitions)
		{
			if (this._state != newState)
			{
				this._state = newState;
				switch (this._state)
				{
				case LoopingSelectorItem.State.Normal:
					VisualStateManager.GoToState(this, "Normal", useTransitions);
					return;
				case LoopingSelectorItem.State.Expanded:
					VisualStateManager.GoToState(this, "Expanded", useTransitions);
					return;
				case LoopingSelectorItem.State.Selected:
					VisualStateManager.GoToState(this, "Selected", useTransitions);
					break;
				default:
					return;
				}
			}
		}
		internal LoopingSelectorItem.State GetState()
		{
			return this._state;
		}
		private void LoopingSelectorItem_Tap(object sender, GestureEventArgs e)
		{
			e.set_Handled(true);
		}
		private void LoopingSelectorItem_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			base.CaptureMouse();
			this._shouldClick = true;
		}
		private void LoopingSelectorItem_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{
			base.ReleaseMouseCapture();
			if (this._shouldClick)
			{
				this._shouldClick = false;
				SafeRaise.Raise(this.Click, this);
			}
		}
		private void LoopingSelectorItem_LostMouseCapture(object sender, MouseEventArgs e)
		{
			this._shouldClick = false;
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this.Transform = ((base.GetTemplateChild("Transform") as TranslateTransform) ?? new TranslateTransform());
		}
		internal void Remove()
		{
			if (this.Previous != null)
			{
				this.Previous.Next = this.Next;
			}
			if (this.Next != null)
			{
				this.Next.Previous = this.Previous;
			}
			this.Next = (this.Previous = null);
		}
		internal void InsertAfter(LoopingSelectorItem after)
		{
			this.Next = after.Next;
			this.Previous = after;
			if (after.Next != null)
			{
				after.Next.Previous = this;
			}
			after.Next = this;
		}
		internal void InsertBefore(LoopingSelectorItem before)
		{
			this.Next = before;
			this.Previous = before.Previous;
			if (before.Previous != null)
			{
				before.Previous.Next = this;
			}
			before.Previous = this;
		}
	}
}
